﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using FantanstPlatform.Decision.CaseXmlService;

namespace CaseXmlService
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            Creat();
            Thread tserver = new Thread(StartServerListen);
            tserver.Start();
        }

        protected override void OnStop()
        {
        }

        private void Creat()
        {
            try
            {
                CaseCountFactory caseFactory = new CaseCountFactory();
                XmlFactory xmlFactory = new XmlFactory();

                DateTime startTime = DateTime.Now;
                DateTime endTime = DateTime.Now;

                List<CaseCount> stationThisYearCase = caseFactory.getThisYearCase(out startTime, out endTime);
                xmlFactory.CreatMultiBarChartXml(stationThisYearCase, "本年各辖区案件统计图", startTime.ToShortDateString() + "--" + endTime.ToShortDateString(), "03");

                List<CaseCount> stationThisMonthCase = caseFactory.getThisMonthCase(out startTime, out endTime);
                xmlFactory.CreatMultiBarChartXml(stationThisMonthCase, "本月各辖区案件统计图", startTime.ToShortDateString() + "--" + endTime.ToShortDateString(), "04");

                List<CaseCount> stationLastMonthCase = caseFactory.getLastMonthCase(out startTime, out endTime);
                xmlFactory.CreatMultiBarChartXml(stationLastMonthCase, "上月各辖区案件统计图", startTime.ToShortDateString() + "--" + endTime.ToShortDateString(), "05");

                List<CaseCount> stationThisWeekCase = caseFactory.getThisWeekCase(out startTime, out endTime);
                xmlFactory.CreatMultiBarChartXml(stationThisWeekCase, "本周各辖区案件统计图", startTime.ToShortDateString() + "--" + endTime.ToShortDateString(), "06");

                List<CaseCount> stationLastWeekCase = caseFactory.getLastWeekrCase(out startTime, out endTime);
                xmlFactory.CreatMultiBarChartXml(stationLastWeekCase, "上周各辖区案件统计图", startTime.ToShortDateString() + "--" + endTime.ToShortDateString(), "07");

                List<ContrastCaseCount> timeTwoCase = caseFactory.getTwoCase(out startTime, out endTime);
                xmlFactory.CreatMultiTendencyChartXml(timeTwoCase, "本年发案趋势图", startTime.ToShortDateString() + "--" + endTime.ToShortDateString(), "01");

                List<ContrastCaseCount> timeFiveCase = caseFactory.getFiveCase(out startTime, out endTime);
                xmlFactory.CreatMultiTendencyChartXml(timeFiveCase, "本年五类案件趋势图", startTime.ToShortDateString() + "--" + endTime.ToShortDateString(), "02");
            }
            catch (Exception ex)
            {
                System.IO.StreamWriter sw = new System.IO.StreamWriter("C://CaseXml//" + DateTime.Now.ToString("yyyyMMdd"), true, Encoding.Default);
                sw.WriteLine("错误提示：" + ex.ToString());
                sw.Close();
                sw = null;
            }
        }

        public void StartServerListen()
        {
            try
            {
                while (true)
                {
                    Thread.Sleep(4 * 60 * 60 * 1000);
                    Creat();
                }
            }
            catch (Exception ex)
            { 
            }
        }
    }
}
